<?php
//数据组-数据库实例
namespace Biz\model\bigData;

/**
 *  浏览热门榜用户信息
 * Class RankingHotUser
 * @package Biz\model\bigData
 */
class RankingHotUser extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_bigdata_online';

    const _CONFIG_SELECT_ = 'bigdata_centre';

    const _DATABASE_ = 'hhz_bigdata_online';

    const _TABLE_NAME = 'look_wiki_ranking_hot_user';


    public static function getUserRanking($page = 1, $pageSize = 200, $uid = null)
    {
        $result = [];
        $limit = [($page - 1) * $pageSize, $pageSize];
        $where = [
            'LIMIT' => $limit,
            'GROUP' => ['uid'],
        ];
        if (!is_null($uid)) {
            $where['uid'] = $uid;
        }
        $list = self::select([
            'uid',
            'ranking' => \Medoo\Medoo::raw("group_concat(ranking_id,'_',max_ts)")
        ], $where);
        foreach ($list as $row) {
            $rankingArr = explode(',', $row['ranking']);
            $userRankings = [];
            foreach ($rankingArr as $arrRow) {
                $arr = explode('_', $arrRow);
                $rankingId = $arr[0];
                $maxTs = $arr[1];
                $userRankings[$rankingId] = $maxTs;
            }
            arsort($userRankings);
            foreach ($userRankings as $rankingId => $maxTs) {
                $result[$row['uid']] = $rankingId;
                break;
            }
        }
        return $result;
    }

}